cmake_minimum_required(VERSION 3.15)
project(FileCollaborationPlatform VERSION 1.0.0 LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 设置输出路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/out/bin/${CMAKE_BUILD_TYPE})  # 可执行文件
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/out/lib/${CMAKE_BUILD_TYPE})  # 动态库
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/out/lib/${CMAKE_BUILD_TYPE})  # 静态库

# 确保多配置生成器也使用相同路径
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
    string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/out/bin/${OUTPUTCONFIG})
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/out/lib/${OUTPUTCONFIG})
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/out/lib/${OUTPUTCONFIG})
endforeach()

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG")

# 查找依赖库
find_package(Boost 1.71 REQUIRED COMPONENTS system thread filesystem)  # Ubuntu 22自带1.74
find_package(Protobuf REQUIRED)

# gRPC (使用库查找方式,兼容Ubuntu 22)
find_library(GRPC_LIB grpc++)
find_library(GRPCPP_LIB grpc++_reflection)
if(NOT GRPC_LIB)
    message(WARNING "gRPC not found, some features will be disabled")
    set(GRPC_LIB "")
    set(GRPCPP_LIB "")
endif()

# nlohmann_json (可选,如果找不到就跳过)
find_package(nlohmann_json 3.2.0 QUIET)
if(NOT nlohmann_json_FOUND)
    message(WARNING "nlohmann_json not found, will use header-only mode")
endif()

# MySQL Connector C++ (可选)
find_library(MYSQLCPPCONN_LIB mysqlcppconn)
if(NOT MYSQLCPPCONN_LIB)
    message(WARNING "MySQL Connector C++ not found, database features will be disabled")
    set(MYSQLCPPCONN_LIB "")
endif()

# Hiredis (可选)
find_library(HIREDIS_LIB hiredis)
if(NOT HIREDIS_LIB)
    message(WARNING "Hiredis not found, Redis features will be disabled")
    set(HIREDIS_LIB "")
endif()

# 包含目录
include_directories(
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/common_component/include()
    ${CMAKE_SOURCE_DIR}/logger
    ${Boost_INCLUDE_DIRS}
    ${PROTOBUF_INCLUDE_DIRS}
)

# 子目录 (暂时只编译logger和common_component)
add_subdirectory(logger)
add_subdirectory(common_component)
add_subdirectory(test)
add_subdirectory(config)
add_subdirectory(framework)
add_subdirectory(gateway)
add_subdirectory(services)
add_subdirectory(database)
# TODO: 后续逐步添加

# add_subdirectory(protocol)
# add_subdirectory(database)

# add_subdirectory(services)
# add_subdirectory(monitor)

# 主程序 - 测试程序
add_executable(cpp_backend_server
    main.cpp
)

# 链接库 (暂时只链接logger和common_component)
target_link_libraries(cpp_backend_server
    logger
    common_component
    pthread
)

# 安装规则
install(TARGETS cpp_backend_server DESTINATION bin)
install(DIRECTORY ${CMAKE_SOURCE_DIR}/config/ DESTINATION etc/cpp_backend_server)

# 测试
enable_testing()
# add_subdirectory(tests)
